<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="产品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNum" >
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="代理商状态" clearable size="small">
          <el-option
            v-for="dict in sourceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">

        <el-button
          type="warning"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出

        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList">
      <el-table-column prop="orderNum" label="订单编号"  width="150" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="goodsName" label="产品名称" width="150" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="payName" label="付款人" width="100"></el-table-column>
      <el-table-column prop="amount" label="金额" width="100"></el-table-column>
      <el-table-column prop="payTime" label="支付时间" width="200" align="center">
        <template slot-scope="scope">
          <span style="color: orange" v-if="scope.row.status == 2">未支付</span>
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="shopName" label="所属商户" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.shopName == '' ">{{scope.row.proxyName}}</span>
          <span>{{scope.row.shopName}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="订单状态" width="80">
        <template slot-scope="scope">
          <span style="color: #3894ff" v-if="scope.row.status == 0">待支付</span>
          <span style="color: #67C23A" v-if="scope.row.status == 1">交易成功</span>
          <span style="color: #ed5565" v-if="scope.row.status == 2">交易失败</span>
          <span style="color: #ed5565" v-if="scope.row.status == 3">超时</span>
        </template>
      </el-table-column>
      <el-table-column prop="source" label="订单来源" align="center">
        <template slot-scope="scope">
          <span  v-if="scope.row.source == 1">pc端</span>
          <span  v-if="scope.row.source == 2">小程序</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="getInfo(scope.row)"
            v-hasPermi="['order:order:query']"
          >查看详情
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="PayStatus(scope.row)"-->

          <!--          >查看支付结果</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="详情"
      :visible.sync="infoOpen"
      @close="cancelOrderInfo"
      width="50%">
      <el-descriptions :column="2">
          <el-descriptions-item label="订单编号">{{infoList.orderNum}}</el-descriptions-item>
          <el-descriptions-item label="产品名称">{{infoList.goodsName}}</el-descriptions-item>
          <el-descriptions-item label="金额">{{infoList.amount}}</el-descriptions-item>
          <el-descriptions-item label="商户名称">{{infoList.shopName}}</el-descriptions-item>
          <el-descriptions-item label="订单来源">
            <span style="color: #ed5565" v-if="infoList.source == 1">pc端</span>
            <span style="color: #ed5565" v-if="infoList.source == 2">小程序</span>
          </el-descriptions-item>
          <el-descriptions-item label="订单状态">
            <span style="color: #3894ff" v-if="infoList.status == 0">待支付</span>
            <span style="color: #3894ff" v-if="infoList.status == 1">交易成功</span>
            <span style="color: #ed5565" v-if="infoList.status == 2">交易失败</span>
            <span style="color: #ed5565" v-if="infoList.status == 3">超时</span>
          </el-descriptions-item>
          <el-descriptions-item label="支付时间">{{infoList.payTime}}</el-descriptions-item>
          <el-descriptions-item label="订单备注">{{infoList.remarks}}</el-descriptions-item>
      </el-descriptions>
      <!-- <el-table
        :data="infoList"
        style="width: 100%">
        <el-table-column
          prop="orderNum"
          label="	订单编号"
          width="180">
        </el-table-column>

        <el-table-column
          prop="goodsName"
          label="产品名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="amount"
          label="	金额"
          width="180">
        </el-table-column>
        <el-table-column
          prop="quantity"
          label="购买数量	">
        </el-table-column>
        <el-table-column
          prop="shopName"
          label="商户名称">
        </el-table-column>
        <el-table-column
          prop="source"
          label="订单来源">
          <template slot-scope="scope">
            <span style="color: #3894ff" v-if="scope.row.source == 1">管理后台</span>
            <span style="color: #ed5565" v-if="scope.row.source == 2">小程序</span>
            <span style="color: #ed5565" v-if="scope.row.source == 3">手机App</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          label="订单来源">
          <template slot-scope="scope">
            <span style="color: #3894ff" v-if="scope.row.status == 0">待支付</span>
            <span style="color: #3894ff" v-if="scope.row.status == 1">交易成功</span>
            <span style="color: #ed5565" v-if="scope.row.status == 2">交易失败</span>
            <span style="color: #ed5565" v-if="scope.row.status == 3">超时</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="remarks"
          label="订单备注">
        </el-table-column>
        <el-table-column
          prop="payTime"
          label="支付时间">
        </el-table-column>

      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="infoOpen = false">确 定</el-button>
      </span> -->
    </el-dialog>
  </div>
</template>

<script>
// import { list, orderInfo, delActing, addActing, orderAdd, listExcludeChild,proxyUserCount,actingStatus } from "@/api/acting/acting";
import { orderAdd, list, orderInfo, getPayStatus } from '@/api/order/list'
import { packagelist } from '@/api/package/package'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { listDeptExcludeChild } from '@/api/system/dept'
import { downLoadExcel } from '@/utils/download'

export default {
  name: 'Dept',
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      orderList: [],
      // 代理商树选项
      actingOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      //页面总条数
      total:0,
      //详情接口弹窗
      infoOpen: false,
      //订单详情信息表格数据
      infoList: [],
      // 状态数据字典
      sourceOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: '上级代理商不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '代理商名称不能为空', trigger: 'blur' }
        ],
        orderNum: [
          { required: true, message: '显示排序不能为空', trigger: 'blur' }
        ],
        email: [
          {
            type: 'email',
            message: '\'请输入正确的邮箱地址',
            trigger: ['blur', 'change']
          }
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ]

      },
      //套餐列表
      comboList: []
    }
  },
  created() {
    this.getList()
    // this.getCombo()
    this.getDicts('order_status').then(response => {
      this.sourceOptions = response.data
      this.sourceOptions.forEach(item => {
        item.dictValue = Number(item.dictValue)
      })
    })
  },
  methods: {
    /** 查询代理商列表 */
    getList() {
      this.loading = true
      list(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;

        this.loading = false
      })
    },
    //查询套餐列表
    getCombo() {
      packagelist().then(res => {
        this.comboList = res.rows
      })
    },

    //查看支付结果
    PayStatus(row) {
      getPayStatus(row.id).then(res => {
        console.log(res)
      })

    },

    //套餐列表选中时将套餐名称赋予form中
    comboChange(e) {
      let getNameObj = this.comboList.find(function(item) {
        return item.id == e
      })
      this.form.goodsName = getNameObj.name
      console.log(this.form.goodsName)
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.sourceOptions, row.status)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    cancelOrderInfo() {
      this.infoOpen = false
      this.infoList = {}
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        parentId: undefined,
        actingName: undefined,
        orderNum: undefined,
        leader: undefined,
        phone: undefined,
        email: undefined,
        status: '0'
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd(row) {

      this.reset()
      if (row != undefined) {
        this.form.parentId = row.id
      }
      this.open = true
      this.title = '订单下单'
      this.form.status = 0
      this.getCombo()
      console.log(this.form)
    },
    //查询订单详情
    getInfo(row) {
      this.infoOpen = true
      orderInfo(row.orderNum).then(res => {
        this.infoList = res.data
        console.log(typeof(this.infoList))
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      downLoadExcel('/order/export', this.queryParams)
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          orderAdd(this.form).then(response => {
            this.msgSuccess('新增成功')
            this.open = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
